<style lang="less">
	.mint-switch-input:checked + .mint-switch-core{
		border-color: #29B6F6;
		background: #29B6F6;
	}
	.yousport-page-setting{
		.title{
			height: 40px;
			line-height: 40px;
			background: #f5f5f5;
			font-size: 13px;
			color: #9E9E9E;
			padding: 0 12px;
		}
		.list-wrap{
			color: #262626;
			background: #fff;
			padding: 0 0 0 12px;
			.item{
				display: -webkit-box;
				-webkit-box-align: center;
				margin: 0;
				padding: 0 12px 0 0;
				list-style: none;
				font-size: 16px;
				height: 50px;
				border-bottom: 1px solid #EDEDED;
				.l{
					-webkit-box-flex: 1;
				}
				.r{
				}
			}
		}
		.btn-wrap{
			margin: 14px 12px;
			.btn{
				border:0;
				outline: 0;
				display: block;
				width: 100%;
				height: 44px;
				color:#fff;
				font-size: 16px;
				border-radius: 4px;
				background: #fc6e6e;
			}
		}
	}
</style>
<template>
	<div class="yousport-page-setting">
		<div class="title">设置仅对当前空间生效</div>
		<div class="list-wrap">
			<ul class="list">
				<li class="item">
					<div class="l">加入排行榜</div>
					<div class="r">
						<mt-switch ref="joinRanking" v-model="isRanking" @change.native="changeJoinRanking"></mt-switch>
					</div>
				</li>
				<li class="item">
					<div class="l">接收排行榜消息</div>
					<div class="r">
						<mt-switch ref="acceptMsg" v-model="isRankingMsg" @change.native="changeAcceptRankingMsg"></mt-switch>
					</div>
				</li>
				<li class="item">
					<div class="l">接收朋友点赞消息</div>
					<div class="r">
						<mt-switch ref="acceptLike" v-model="isLikeMsg" @change.native="changeAcceptLikeMsg"></mt-switch>
					</div>
				</li>
			</ul>
		</div>
		<div class="btn-wrap"><button type="button" class="btn" @click="disableAllService">停用此功能</button></div>
	</div>
</template>
<script>
	import config from '../assets/config'
	import { MessageBox } from 'mint-ui'
    import bridge from '../bridge'

	export default {
		data(){
			return {
				isRanking: true,
				isRankingMsg: true,
				isLikeMsg: true,
				isEnabled: true
			};
		},
		activated(){
			this.initSwitchStatus();
			this.initHeader();
		},
		methods:{
			initSwitchStatus(){
				this.$http.get(config.userInfo).then((res)=>{
					if(res.data.code === 0){
						let userInfo = res.data.data.userInfo;
						this.isRanking = !!userInfo.isRanking;
						this.isRankingMsg = !!userInfo.isRankingMsg;
						this.isLikeMsg = !!userInfo.isLikeMsg;
						this.isEnabled = !!userInfo.isEnabled;
					}
				});
			},
			changeJoinRanking(){
				this.$http.post(config.settingJoinRanking).then(res => {
					if(res.data.code === 0){
						let data = res.data.data;
						this.setConfig(data);
					}
				});
			},
			changeAcceptRankingMsg(){
				this.$http.post(config.settingAcceptRankingMsg).then(res => {
					if(res.data.code === 0){
						let data = res.data.data;
						this.setConfig(data);
					}
				});;
			},
			changeAcceptLikeMsg(){
				this.$http.post(config.settingAcceptLikeMsg).then(res => {
					if(res.data.code === 0){
						let data = res.data.data;
						this.setConfig(data);
					}
				});;
			},
			disableAllService(){
				MessageBox({
                    title:'',
                    message: '确定要停用友运动吗？',
					showCancelButton: true
                }).then(action =>{
                	if(action == 'confirm'){
						this.$http.post(config.settingDisableAllService).then(res =>{
							if(res.data.code === 0){
								let data = res.data.data;
								this.setConfig(data);
								this.$router.push({path: '/?disable=true'});
							}
						});
                	}
                });
			},
			initHeader() {
                bridge.client.configNavBar(function () {
                }, {centerItems:[{title:'友运动设置'}],rightItems:[]});
            },
            setConfig(data){
            	this.$root.isEnabled = !!data.isEnabled;
            	this.$root.isRanking = !!data.isRanking;
            	this.$root.isRankingMsg = !!data.isRankingMsg;
            	this.$root.isLikeMsg = !!data.isLikeMsg;
            }
		}
	}
</script>